﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using DataControls;

namespace GiaoVien_QuanLy_DeTai
{
    public partial class CHUCVU_Form : Form
    {
        DataBaseAccess dataBaseAccess = new DataBaseAccess();
        CV_GIAOVIEN cv_giaovien = new CV_GIAOVIEN();
        CHUCVU chucvu = new CHUCVU();
        GIAOVIEN_Data giaovien_Data = new GIAOVIEN_Data();
        CHUCVU_Data chucvu_Data = new CHUCVU_Data();

        public CHUCVU_Form()
        {
            InitializeComponent();
        }

        private void CHUCVU_Form_Load(object sender, EventArgs e)
        {
            txtLuongCV.Text = "";
            txtMaCV.Text = "CV";
            txtTenChucVu.Text = "";

            cbChucVu.Items.Clear();
            cbGiaoVien.Items.Clear();

            chucvu_Data.chucvu_Combobox(cbChucVu);
            giaovien_Data.giaovien_Combobox(cbGiaoVien);
        }

        private void btnHienThi_Click(object sender, EventArgs e)
        {
            chucvu.hienthi_ChucVu(dtgChucVu);

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaCV.Text == "")
            {
                MessageBox.Show("Ban chua dien Ma Chuc Vu");
                txtMaCV.Focus();
                return;
            }
            else if (txtTenChucVu.Text == "")
            {
                MessageBox.Show("Ban chua dien Ten Chuc Vu");
                txtTenChucVu.Focus();
                return;
            }
            else if (txtTenChucVu.Text == "" )
            {
                MessageBox.Show("Ban chua dien Luong Chuc Vu");
                txtLuongCV.Focus();
                return;
            }
            else if (chucvu_Data.kiemtra_MaChucVu(txtMaCV.Text.ToString()))
            {
                MessageBox.Show("Ma Chuc Vu ton tai");
                txtMaCV.Focus();
                return;
            }
            else if (chucvu_Data.kiemtra_ChucVu(txtTenChucVu.Text.ToString()))
            {
                MessageBox.Show("Ten Chuc Vu ton tai");
                txtTenChucVu.Focus();
                return;
            }
            else
            {
                chucvu.maCV = txtMaCV.Text.ToString();
                chucvu.tenCV = txtTenChucVu.Text.ToString();

                try
                {
                    if ((chucvu.luongCV = (float)(Convert.ToDouble(txtLuongCV.Text))) > 0)
                    {
                        chucvu.them_ChucVu(chucvu);
                        chucvu.hienthi_ChucVu(dtgChucVu);
                    }
                    else
                    {
                        MessageBox.Show("Ban Chua Nhap Dung luong chuc vu");
                        txtLuongCV.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung luong chuc vu \n" + ex.Message);
                }
                txtLuongCV.Text = "";
                txtMaCV.Text = "CV";
                txtTenChucVu.Text = "";

                cbChucVu.Items.Clear();
                cbGiaoVien.Items.Clear();

                chucvu_Data.chucvu_Combobox(cbChucVu);
                giaovien_Data.giaovien_Combobox(cbGiaoVien);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaCV.Text == "")
            {
                MessageBox.Show("Ban chua dien Ma Chuc Vu");
                txtMaCV.Focus();
                return;
            }
            else if (txtTenChucVu.Text == "")
            {
                MessageBox.Show("Ban chua dien Ten Chuc Vu");
                txtTenChucVu.Focus();
                return;
            }
            else if (txtTenChucVu.Text == "")
            {
                MessageBox.Show("Ban chua dien Luong Chuc Vu");
                txtLuongCV.Focus();
                return;
            }
            else
            {
                chucvu.maCV = txtMaCV.Text.ToString();
                chucvu.tenCV = txtTenChucVu.Text.ToString();

                try
                {
                    if ((chucvu.luongCV = (float)(Convert.ToDouble(txtLuongCV.Text))) > 0)
                    {
                        chucvu.sua_ChucVu(chucvu);
                        chucvu.hienthi_ChucVu(dtgChucVu);
                    }
                    else
                    {
                        MessageBox.Show("Ban Chua Nhap Dung luong chuc vu");
                        txtLuongCV.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung luong chuc vu \n" + ex.Message);
                }
                txtLuongCV.Text = "";
                txtMaCV.Text = "CV";
                txtTenChucVu.Text = "";

                cbChucVu.Items.Clear();
                cbGiaoVien.Items.Clear();

                chucvu_Data.chucvu_Combobox(cbChucVu);
                giaovien_Data.giaovien_Combobox(cbGiaoVien);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaCV.Text == "")
            {
                MessageBox.Show("Ban chua Nhap Ma Chuc Vu");
                txtMaCV.Focus();
                return;
            }
            else if (chucvu_Data.kiemtraMaCV_CVGV(txtMaCV.Text.ToString()))
            {
                MessageBox.Show("Ban chua xoa Chuc Vu Trong Chuc Vu Giao Vien");
                return;
            }
            else
            {
                chucvu.xoa_ChucVu(txtMaCV.Text.ToString());
                chucvu.hienthi_ChucVu(dtgChucVu);
            }
            cbChucVu.Items.Clear();
            cbGiaoVien.Items.Clear();

            chucvu_Data.chucvu_Combobox(cbChucVu);
            giaovien_Data.giaovien_Combobox(cbGiaoVien);
        }

        private void btnHienThiCV_Click(object sender, EventArgs e)
        {
            cv_giaovien.hienthi_ChucVuGiaoVien(dtgChucVu_GiaoVien);
        }

        private void btnThemCV_Click(object sender, EventArgs e)
        {
            if (cbGiaoVien.Text == "")
            {
                MessageBox.Show("Ban chua chon giao vien");
            }
            else if (cbChucVu.Text == "")
            {
                MessageBox.Show("Ban chua chon chuc vu");
            }
            else
            {
                cv_giaovien.maCV = ((ListItem)(cbChucVu.SelectedItem)).value;
                cv_giaovien.maGV = ((ListItem)(cbGiaoVien.SelectedItem)).value;
                cv_giaovien.them_CVGiaoVien(cv_giaovien);
                cv_giaovien.hienthi_ChucVuGiaoVien(dtgChucVu_GiaoVien);

                cbChucVu.Items.Clear();
                cbGiaoVien.Items.Clear();

                chucvu_Data.chucvu_Combobox(cbChucVu);
                giaovien_Data.giaovien_Combobox(cbGiaoVien);
            }


        }

        private void btnSuaCV_Click(object sender, EventArgs e)
        {
            if (cbGiaoVien.Text == "")
            {
                MessageBox.Show("Ban chua chon giao vien");
            }
            else if (cbChucVu.Text == "")
            {
                MessageBox.Show("Ban chua chon chuc vu");
            }
            else
            {
                cv_giaovien.maCV = ((ListItem)(cbChucVu.SelectedItem)).value;
                cv_giaovien.maGV = ((ListItem)(cbGiaoVien.SelectedItem)).value;
                cv_giaovien.sua_CVGiaoVien(cv_giaovien);
                cv_giaovien.hienthi_ChucVuGiaoVien(dtgChucVu_GiaoVien);

                cbChucVu.Items.Clear();
                cbGiaoVien.Items.Clear();

                chucvu_Data.chucvu_Combobox(cbChucVu);
                giaovien_Data.giaovien_Combobox(cbGiaoVien);
            }

        }

        private void btnXoaCV_Click(object sender, EventArgs e)
        {
            if (cbGiaoVien.Text == "")
            {
                MessageBox.Show("Ban chua chon giao vien");
            }
            else
            {
                cv_giaovien.maGV = ((ListItem)(cbGiaoVien.SelectedItem)).value;
                cv_giaovien.xoa_CVGiaoVien(cv_giaovien.maGV);
                cv_giaovien.hienthi_ChucVuGiaoVien(dtgChucVu_GiaoVien);

            }
            cbChucVu.Items.Clear();
            cbGiaoVien.Items.Clear();

            chucvu_Data.chucvu_Combobox(cbChucVu);
            giaovien_Data.giaovien_Combobox(cbGiaoVien);
        }

        private void dtgChucVu_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dtgChucVu.AllowUserToAddRows = false;
            int dong = e.RowIndex;
            txtMaCV.Text = dtgChucVu.Rows[dong].Cells[0].Value.ToString();
            txtTenChucVu.Text = dtgChucVu.Rows[dong].Cells[1].Value.ToString();
            txtLuongCV.Text = dtgChucVu.Rows[dong].Cells[2].Value.ToString();


            
        }

        private void dtgChucVu_GiaoVien_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int dong = e.RowIndex;
            
            
            

            String magiaovien = dtgChucVu_GiaoVien.Rows[dong].Cells[0].Value.ToString();
            chucvu_Data.layten_GiaoVien(magiaovien, cbGiaoVien);

            String machucvu = dtgChucVu_GiaoVien.Rows[dong].Cells[1].Value.ToString();       
            chucvu_Data.layten_ChucVu(machucvu, cbChucVu);
        }

        private void txtMaCV_MouseClick(object sender, MouseEventArgs e)
        {
            txtLuongCV.Text = "";
            txtMaCV.Text = "CV";
            txtTenChucVu.Text = "";
        }




    }
}
